import javax.swing.*;
import java.awt.*;

/**
 * @author Huhao
 * &#064;date  2024/11/10
 */

public class AlgorithmCheck extends JFrame {
    private JTextArea richTextBox1;
    private JLabel label2;

    public AlgorithmCheck() {
        initialize();
    }

    private void initialize() {
        setTitle("算术表达式检验");
        setLocation(400,200);
        setSize(869, 566);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(null);
        getContentPane().setBackground(new Color(240, 240, 240));

        JLabel label1 = new JLabel("算术表达式检验");
        label1.setFont(new Font("微软雅黑", Font.BOLD, 42));
        label1.setBounds(260, 20, 520, 70);
        label1.setForeground(new Color(50, 50, 50));
        add(label1);

        JPanel groupBox1 = new JPanel();
        groupBox1.setBounds(12, 106, 845, 207);
        groupBox1.setLayout(null);
        groupBox1.setBackground(new Color(255, 255, 255));
        groupBox1.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 2));
        add(groupBox1);

        richTextBox1 = new JTextArea();
        richTextBox1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        richTextBox1.setBounds(127, 78, 571, 53);
        richTextBox1.setLineWrap(true);
        richTextBox1.setWrapStyleWord(true);
        richTextBox1.setBorder(BorderFactory.createLineBorder(new Color(150, 150, 150), 1));
        richTextBox1.addCaretListener(e -> label2.setText(""));
        groupBox1.add(richTextBox1);

        JButton button1 = new JButton("开始检验");
        button1.setBounds(351, 137, 108, 50);
        button1.setBackground(new Color(70, 130, 180));
        button1.setForeground(Color.WHITE);
        button1.setFont(new Font("微软雅黑", Font.BOLD, 16));
        button1.setFocusPainted(false);
        button1.addActionListener(e -> button1_Click());
        groupBox1.add(button1);

        JPanel groupBox2 = new JPanel();
        groupBox2.setBounds(12, 319, 845, 235);
        groupBox2.setLayout(null);
        groupBox2.setBackground(new Color(255, 255, 255));
        groupBox2.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 2));
        add(groupBox2);

        label2 = new JLabel();
        label2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        label2.setBounds(6, 105, 833, 56);
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        groupBox2.add(label2);

        JLabel label3 = new JLabel("输入四则运算算术表达式，可测试其是否符合C++语言标准");
        label3.setBounds(220, 41, 519, 20);
        label3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        label3.setForeground(new Color(100, 100, 100));
        groupBox1.add(label3);

        setVisible(true);
    }

    private void button1_Click() {
        label2.setText("");
        String text = richTextBox1.getText();
        StringBuilder text2 = new StringBuilder();
        char[] source = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        char[] source2 = {'+', '-', '*', '/'};
        char[] source4 = {'(', ')'};
        int num = 0;

        for (num = 0; num < text.length(); ) {
            char current = text.charAt(num);
            if (Character.isLetter(current) || current == '_') {
                while (num < text.length() && (Character.isLetter(text.charAt(num)) || text.charAt(num) == '_' || Character.isDigit(text.charAt(num)))) {
                    num++;
                }
                text2.append("0");
            } else if (new String(source).indexOf(current) >= 0) {
                int index = num;
                int num2 = 0;
                while (num < text.length() && new String(source).indexOf(text.charAt(num)) >= 0) {
                    num++;
                    num2++;
                }
                if (text.charAt(index) == '0' && num2 > 1) {
                    label2.setText("错误的计算机算术表达式");
                    return;
                }
                text2.append("0");
            } else if (new String(source2).indexOf(current) >= 0) {
                StringBuilder temp = new StringBuilder();
                while (num < text.length() && check(temp.toString() + text.charAt(num))) {
                    temp.append(text.charAt(num));
                    num++;
                }
                text2.append((temp.charAt(0) != '*' && temp.charAt(0) != '/') ? "3" : "2");
            } else if (new String(source4).indexOf(current) >= 0) {
                text2.append(current == '(' ? "5" : "6");
                num++;
            } else {
                if (current != ' ') {
                    label2.setText("错误的计算机算术表达式");
                    return;
                }
                num++;
            }
        }

        if (num >= text.length()) {
            String newText;
            do {
                newText = text2.toString();
                text2 = new StringBuilder(newText.replace("506", "0")
                        .replace("020", "0")
                        .replace("030", "0")
                        .replace("5306", "0"));
            } while (!newText.equals(text2.toString()));

            if (text2.length() == 1) {
                label2.setText("正确的计算机算术表达式");
            } else if (text.length() == 0) {
                label2.setText("无表达式");
            } else {
                label2.setText("错误的计算机算术表达式");
            }
        }
    }

    public boolean check(String data) {
        if (data.length() > 1) {
            if (data.charAt(0) == '+' || data.charAt(0) == '-') {
                for (int i = 1; i < data.length(); i++) {
                    if (data.charAt(i) == data.charAt(i - 1) || (data.charAt(i) != '+' && data.charAt(i) != '-')) {
                        return false;
                    }
                }
                return true;
            }
            if (data.charAt(0) == '*' || data.charAt(0) == '/') {
                if (data.charAt(1) == '+' || data.charAt(1) == '-') {
                    for (int j = 2; j < data.length(); j++) {
                        if (data.charAt(j) == data.charAt(j - 1) || (data.charAt(j) != '+' && data.charAt(j) != '-')) {
                            return false;
                        }
                    }
                    return true;
                }
                return false;
            }
            return false;
        }
        return true;
    }
}
